package com.pz.gulimall.order.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.pz.gulimall.common.to.OrderInfoTo;
import com.pz.gulimall.common.to.OrderStatusTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.order.entity.OrderEntity;
import com.pz.gulimall.order.service.OrderService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.validation.Valid;


/**
 * 订单
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-02 11:30:41
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 保存客户端的订单数据（前提保证选购的商品有货）
     * @param orderInfoTo 订单数据模型
     * @return {orderId:, outOfStockSkuIds:[]}
     */
    @PostMapping("/saveOrder")
    public R saveOrder(@RequestBody @Valid OrderInfoTo orderInfoTo){
        return orderService.saveOrder(orderInfoTo);
    }

    /**
     *  主动取消订单
     * @param orderId
     */
    @GetMapping("/cancel-order/{orderId}")
    public R cancelOrder(@PathVariable Long orderId) {
        orderService.cancelOrder(orderId);
        return R.ok("取消成功");
    }

    /**
     * 批量获取订单状态
     */
    @PostMapping("get-orderStatus")
    private R getOrderStatus(@RequestBody List<Long> orderIds){
        List<OrderEntity> orderEntities = orderService.listByIds(orderIds);
        List<OrderStatusTo> orderStatusTos = new ArrayList<>();
        orderEntities.forEach(item -> {
            OrderStatusTo statusTo = new OrderStatusTo();
            BeanUtils.copyProperties(item, statusTo);
            statusTo.setOrderId(item.getId());
            orderStatusTos.add(statusTo);
        });
        return R.ok().put("data", orderStatusTos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
